﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Component.BLL;
using OF.Component.Model;
using OF.Utility;
using OpSystem.common;

namespace OpSystem.manage.authority
{
    public partial class AuthorityMenuEdit : BasePage
    {
        protected int intID = 0;
        protected int intCID = 0;
        protected int intSystemID = 0;
        protected string strMessage = "";
        protected string strStyle= "style='display:none;'";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                intID = RequestUtility.GetQueryInt("ID", -1);
                intCID = RequestUtility.GetQueryInt("cid", -1);
                BindDictionary();
                if (intID > -1)
                {
                    AuthorityMenuInfo info = AuthorityMenuOperate.GetByID(intID);
                    this.txtName.Text = info.MenuName;
                    this.txtURL.Text = info.MenuUrl;
                    this.txtOrderNum.Text = info.OrderNum.ToString();
                    this.txtMenuCode.Text = info.MenuCode.ToString();
                    this.ddParentRole.SelectedValue = info.ParentID.ToString();
                    if (info.ParentID > 0)
                    {
                        this.ddParentRole.Enabled = true;
                    }
                    else
                    {
                        this.ddParentRole.Enabled = false;
                    }

                }
                else
                {
                    this.ddParentRole.SelectedValue = intCID.ToString();
                    this.ddParentRole.Enabled = false;
                }

            }
        }

        private void BindDictionary()
        {
            int count = 0;
            string where = "1=1 and IsActive=1 and ParentID=0";
            IList<AuthorityMenuInfo> list = AuthorityMenuOperate.Search(out count, 1, 9999, where, "OrderNum", false);
            this.ddParentRole.DataSource = list;
            this.ddParentRole.DataTextField = "MenuName";
            this.ddParentRole.DataValueField = "MenuID";
            this.ddParentRole.DataBind();
            ddParentRole.Items.Insert(0, new ListItem("一级菜单", "0"));
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string MessageInfo = "信息修改成功！";
            intSystemID = RequestUtility.GetQueryInt("sysid", -1);
            intID = RequestUtility.GetQueryInt("ID", -1);
            int parentId = EConvert.ToInt(this.ddParentRole.SelectedValue);
            AuthorityMenuInfo info;
            info = AuthorityMenuOperate.GetByID(intID);
            if (info == null)
            {
                info = new AuthorityMenuInfo();

            }
            AuthorityMenuInfo oldinfo = ObjectCopy<AuthorityMenuInfo>.DeepCopy(info);
            info.MenuName = txtName.Text;
            info.MenuUrl = txtURL.Text;
            info.OrderNum = EConvert.ToInt(txtOrderNum.Text);
            info.ParentID = EConvert.ToInt(this.ddParentRole.SelectedValue);
            info.MenuID = intID;
            info.MenuCode = txtMenuCode.Text;
            info.IsActive = 1;
            info.UpdateTime = DateTime.Now;
            info.SystemID = intSystemID;
            if (info.MenuID > 0)
            {
                AuthorityMenuOperate.Update(info);
                //  OpOperationLogOperate.SetLog(oldinfo, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpMenuManage, info.MenuID);
            }
            else
            {
                MessageInfo = "信息创建成功！";
                info.MenuID = AuthorityMenuOperate.GetMaxID();
                info.CreatedDate = DateTime.Now;
                AuthorityMenuOperate.Insert(info);
                //  OpOperationLogOperate.SetLog(null, info, OpUserID, OpUserInfo.OpUserName, (short)LogType.OpMenuManage, info.MenuID);
            }
            strMessage = " BMCW.msg(\"" + MessageInfo + "\",\"" + GetPageUrl("AuthorityMenuList.aspx?sysid=" + intSystemID) + "\",true);";
        }
    }
}
